﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using NHibernate;
using NHibernate.Criterion;

using BOSS.Framework.Search;
using BOSS.Framework.Search.Context;
using BOSS.Dao.Customers;
using BOSS.Domain.Customers;


namespace BOSS.Dao.NHibernate.Customers
{
    public class CustomerDao : EntityDao<Customer, int>, ICustomerDao
    {
        #region ISearchSupport<Customer> 成员

        public IList<Customer> SelectBySearch(ISearchContext context, out int totalCount)
        {
            int count = 0;

            string name = context.GetFilterTermValueAsString("Name");
            string serialNumberOrName = context.GetFilterTermValueAsString("SerialNumberOrName");
            string mobile = context.GetFilterTermValueAsString("Mobile");

            IList<Customer> customers = this.HibernateTemplate.ExecuteFind<Customer>(delegate(ISession session)
            {
                ICriteria criteria = session.CreateCriteria(typeof(Customer), "c");

                if (!string.IsNullOrEmpty(name))
                {
                    criteria.Add(Expression.Like("c.Name", name, MatchMode.Anywhere));
                }
                if (!string.IsNullOrEmpty(serialNumberOrName))
                {
                    criteria.Add(Expression.Or(Expression.Like("c.Name", serialNumberOrName, MatchMode.Anywhere), Expression.Like("c.SerialNumber", serialNumberOrName, MatchMode.Anywhere)));    
                }
                if (!string.IsNullOrEmpty(mobile))
                {
                    criteria.Add(Expression.Like("c.Mobile", mobile, MatchMode.Anywhere));
                }

                count = criteria.SetProjection(Projections.Count("Id").SetDistinct()).UniqueResult<int>();
                if (count == 0)
                {
                    return new List<Customer>();
                }

                criteria.SetProjection(new IProjection[] { null });
                criteria.SetResultTransformer(CriteriaSpecification.DistinctRootEntity);

                return criteria.SetFirstResult(context.PageSize * (context.PageIndex - 1))
                        .SetMaxResults(context.PageSize)
                        .List<Customer>();
            });

            totalCount = count;
            return customers;
        }

        #endregion

        #region ISerialNumberGeneratorDao 成员

        /// <summary>
        /// 
        /// </summary>
        /// <param name="createDate"></param>
        /// <returns></returns>
        public int CountByCreateDate(DateTime createDate)
        {
            return this.HibernateTemplate.Execute<int>(delegate(ISession session)
            {
                return session.CreateSQLQuery("SELECT COUNT(*) FROM Customers WHERE CONVERT(CHAR(10), CreateTime, 20) = CONVERT(CHAR(10), ?, 20)")
                    .SetCacheable(false)
                    .SetDateTime(0, createDate)
                    .UniqueResult<int>();
            });
        }

        #endregion
    }
}
